var MainPage_Health = window.NameSpace || {};

MainPage_Health.createCircle = function(divId) {
	var dom = document.getElementById(divId);
	MP_GL_HEALTH = echarts.init(dom);
	
	
	MainPage_Health.loadHealth();
	
	return MP_GL_HEALTH;
}


MainPage_Health.loadHealth = function(){
	var labelTop = {
			normal : {
				label : {
					show : false,
					position : 'center',
					formatter : '{b}',
					textStyle : {
						baseline : 'bottom'
					}
				},
				labelLine : {
					show : false
				}
			}
		};
		var labelFromatter = {
			normal : {
				label : {
					show : true,
					position : 'center',
					formatter : function(params) {
						return 100 - params.value + '%'
					},
					textStyle : {
						color : '#666',
						fontSize : 32,
						align : 'center'
					}
				}
			},
		}
		var lableStyle = {
			normal : {
				color : '#eee',
				labelLine : {
					show : false
				}

			},
			emphasis : {
				color : 'rgba(0,0,0,0)'
			}
		};
		var radius = [ '35%', '45%' ];
		var option = {
			color : [ '#00a1ff', '#ffae00', '#4ed852', '#ed7fbd' ],
			legend : {
				x : 'center',
				y : '70%',
				data : [ '设备可用性', '设备健康度', '网络可用性', '网络健康度' ],
				textStyle : {
					color : '#666',
					fontSize : 12
				}
			}
		};

		$.ajax({
			url : "/mainpage/loadHealth",
			async : true,
			success : function(data) {
				MP_GL_HEALTH.showLoading();
				var series = [];
				// 设备可用性
				var  device_use= {
					type : 'pie',
					center : [ '12.5%', '35%' ],
					radius : radius,
					x : 'center', // for funnel
					itemStyle : labelFromatter,
					data : [ {
						name : 'other',
						value : 100-data.device_use,
						itemStyle : lableStyle
					}, {
						name : '设备可用性',
						value : data.device_use,
						itemStyle : labelTop
					} ]
				};
				
				// 设备健康度
				var device_health= {
						type : 'pie',
						center : [ '37.5%', '35%' ],
						radius : radius,
						x : 'center', // for funnel
						itemStyle : labelFromatter,
						data : [ {
							name : 'other',
							value : 100-data.device_health,
							itemStyle : lableStyle
						}, {
							name : '设备健康度',
							value : data.device_health,
							itemStyle : labelTop
						} ]
					};
				
				// 网络可用性
				var net_use = {
					type : 'pie',
					center : [ '62.5%', '35%' ],
					radius : radius,
					x : 'center', // for funnel
					itemStyle : labelFromatter,
					data : [ {
						name : 'other',
						value : 100-data.net_use,
						itemStyle : lableStyle
					}, {
						name : '网络可用性',
						value : data.net_use,
						itemStyle : labelTop
					} ]
				};
				
				// 网络健康度
				var net_health ={
						type : 'pie',
						center : [ '87.5%', '35%' ],
						radius : radius,
						x : 'center', // for funnel
						itemStyle : labelFromatter,
						data : [ {
							name : 'other',
							value : 100-data.net_health,
							itemStyle : lableStyle
						}, {
							name : '网络健康度',
							value : data.net_health,
							itemStyle : labelTop
						}]
				};
				
				series.push(device_use,device_health,net_use,net_health);
				
				option.series =series; 
				MP_GL_HEALTH.setOption(option, true);
				MP_GL_HEALTH.hideLoading();	
				
				// 设置定时刷新任务
				setTimeout(MainPage_Health.loadHealth, mp_refresh_time);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				console.log('exception>>'+textStatus);
				setTimeout(MainPage_Health.loadHealth , mp_refresh_time);
			}
		});
	
}